export function useFetchList(fun: any, query: any) {
  const queryDataFun = fun
  const pagination = reactive({
    total: 0,
    current: 1,
    pageSize: 10,
    showPageSize: true
  })
  const tableData = ref<any[]>([])
  const loading = ref(false)
  const handleSearch = async () => {
    loading.value = true
    queryDataFun({
      ...query,
      pageIndex: pagination.current,
      pageSize: pagination.pageSize
    })
      .then((res: any) => {
        loading.value = false
        tableData.value = res.data.list || res.data
        // pagination.current = res.data.pageNum
        pagination.total = res.data.total
      })
      .finally(() => {
        loading.value = false
      })
  }

  const handleTableChange = (pageInfo: any) => {
    Object.assign(pagination, pageInfo)
    handleSearch()
  }

  const initTable = () => {
    pagination.current = 1
    pagination.pageSize = 10
    handleSearch()
  }

  // onMounted(() => {
  //   handleSearch()
  // })

  return {
    tableData,
    handleTableChange,
    handleSearch,
    pagination,
    loading,
    initTable
  }
}
